package com.edol.painting.main;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.edol.painting.PainterConfig;
import com.edol.painting.R;
import com.edol.painting.utils.Constant;
import com.edol.painting.utils.TimeUtil;


/*
 * 功能描述：意见反馈
 * 作者：黄勇
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
public class FeedbackActivity extends Activity implements View.OnClickListener {
    private EditText mFeedbackEt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_feedback);

        mFeedbackEt = (EditText) findViewById(R.id.feedback_input);
        mFeedbackEt.setHint(getString(R.string.feedback_tips, getString(R.string.app_name)));

        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.submit_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.submit_btn:
                //submitFeedback();
                break;
        }
    }

    private void joinQQ() {
        try {
            String url = "mqqapi://card/show_pslcard?src_type=internal&version=1&uin=314343107&card_type=group&source=qrcode";
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "没有安装QQ!", Toast.LENGTH_SHORT).show();
        }
    }

    /*private void submitFeedback() {
        if (!isCanSubmitFeedback()) {
            String tips = getString(R.string.feedback_frequency, Constant.FEEDBACK_SUBMIT_LIMIT);
            Toast.makeText(FeedbackActivity.this, tips, Toast.LENGTH_SHORT).show();
            return;
        }

        String fbkContent = mFeedbackEt.getText().toString();
        if (TextUtils.isEmpty(fbkContent)) {
            Toast.makeText(FeedbackActivity.this, R.string.feedback_must_be_not_null, Toast.LENGTH_SHORT).show();
            return;
        }

        Feedback feedback = new Feedback();
        fillBaseInfo(feedback);
        feedback.desc = fbkContent;

        final ProgressHUD progressHUD = ProgressHUD.create(this);
        progressHUD.show();
        feedback.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, final BmobException e) {
                progressHUD.dismiss();
                if (e == null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            SharedPreferences preferences = getSharedPreferences(PainterConfig.PACKAGE_NAME, 0);
                            int submitCount = preferences.getInt(Constant.FEEDBACK_SUBMIT_COUNT, 0);
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putLong(Constant.FEEDBACK_SUBMIT_LAST_TIME, System.currentTimeMillis());
                            editor.putInt(Constant.FEEDBACK_SUBMIT_COUNT, submitCount + 1);
                            editor.apply();

                            Toast.makeText(FeedbackActivity.this, "提交意见反馈成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });
                } else {
                    Toast.makeText(FeedbackActivity.this, "提交意见反馈失败，请重新提交!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void fillBaseInfo(Feedback feedback) {
        try {
            String pkName = getPackageName();
            PackageInfo packageInfo = getPackageManager().getPackageInfo(pkName, 0);
            feedback.verName = packageInfo.versionName;
            feedback.verCode = packageInfo.versionCode;

            TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
            feedback.imei = tm.getDeviceId();
            feedback.userId = tm.getLine1Number();

            feedback.appPlatform = "android";
            feedback.deviceName = android.os.Build.MODEL;
            feedback.releaseVer = android.os.Build.VERSION.RELEASE;
        } catch (Exception e) {
        }
    }*/

    private boolean isCanSubmitFeedback() {
        SharedPreferences preferences = getSharedPreferences(PainterConfig.PACKAGE_NAME, 0);
        long lastTime = preferences.getLong(Constant.FEEDBACK_SUBMIT_LAST_TIME, System.currentTimeMillis());
        long currentTime = System.currentTimeMillis();

        boolean isSameDay = TimeUtil.isSameDay(lastTime, currentTime);

        if (!isSameDay) {
            preferences.edit().putInt(Constant.FEEDBACK_SUBMIT_COUNT, 0).apply();
            return true;
        }

        int submitCount = preferences.getInt(Constant.FEEDBACK_SUBMIT_COUNT, 0);
        if (submitCount >= Constant.FEEDBACK_SUBMIT_LIMIT) {
            return false;
        }

        return true;
    }
}
